package com.kidget;


import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class KidgetProvider extends AppWidgetProvider {
	public static final String WIDGETTAG = "Kidget";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
//		super.onUpdate(context, appWidgetManager, appWidgetIds);
//
//	    Log.i(WIDGETTAG, "onUpdate");
//
//		final int N = appWidgetIds.length;
//
//		// Perform this loop procedure for each App Widget that belongs to this provider
//		for (int i=0; i<N; i++) {
//		    int appWidgetId = appWidgetIds[i];
//
//		    Log.i(WIDGETTAG, "updating widget[id] " + appWidgetId);
//
//		    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
//
//		    Intent intent = new Intent(context, KidgetService.class);
//		    intent.setAction(KidgetService.UPDATE_KIDGET);
//		    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//		    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
//		    views.setOnClickPendingIntent(R.id.widgetBtn, pendingIntent);
//		    Log.i(WIDGETTAG, "pending intent set");
//
//		    // Tell the AppWidgetManager to perform an update on the current App Widget
//		    appWidgetManager.updateAppWidget(appWidgetId, views);
//		}
		
	    Log.w(WIDGETTAG, "onUpdate method called");
	    // Get all ids
	    ComponentName thisWidget = new ComponentName(context, KidgetProvider.class);
	    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

	    // Build the intent to call the service
	    Intent intent = new Intent(context.getApplicationContext(), KidgetService.class);
	    intent.setAction(KidgetService.UPDATE_KIDGET);
	    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

	    // Update the widgets via the service
	    context.startService(intent);
	}	
}